<template>
  <div class="page" id="project">
    <div class="flex ym5">
      <el-button class="xm5" type="primary" @click= "change(1)">更换</el-button>
      <el-input class="flex1" v-model.trim="url" ></el-input>
    </div>  
    <div class="flex ym5">
      <el-button class="xm5" type="primary" @click= "change(2)">同步</el-button>
      <el-input class="flex1" v-model.trim="stocks" ></el-input>
    </div>  
    <div class="flex ym5">
      <el-switch class="xm5"  v-model="start" @change= "change"/>
      <div class="flex1">定时推送</div>
    </div>  
    <el-table :data="tableData" border
      style="width: 100%" @cell-click="cellClick" size="small">
      <el-table-column prop="code" label="股票" width="180" />
      <el-table-column prop="avg" label="平均" />
      <el-table-column prop="record" label="记录" />

    </el-table> 
  </div>
</template>
<script> 
import service from "../../common/api.js";
var res;
export default {
  data() {
    return {
      url:'',
      stocks:'',
      start:true,
      tableData: [],
    };
  }, 
  created() { 
    this.load()
  },
  methods: {
    async change(v){
      var stockMap = {}
      this.stocks.split(",").map(e=>{ stockMap[e] = 1 })
      if(v==1 || v==2 ){
        res = await service.post("/stock/api/stock",{ url:this.url,stockMap });
      }else{
        res = await service.post("/stock/api/task",{ runner:v });
      }
       
      this.load()
    },
    async load() {
      res = await service.post("/stock/api/stock", {});
      this.url = res.data.url
      this.stocks = Object.keys(res.data.stockMap).join()
      var data = []
      var map = res.data.recordMap
      Object.keys(map).map(e=>{
          data.push({code:e,avg:map[e][0],record:map[e] })
      })
      this.tableData = data
      
    },
    
  },
};
</script>
<style scoped>
.page {
  padding: 10px;
  height: calc(100vh - 100px);
  cursor: pointer;
  overflow: auto;
}
</style>
